﻿using System;
using System.Collections;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.APP.App_UI.Commons;
using System.Collections.Generic;
using KPIS.GERP.GAMS.APP.Reports;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ReportLedger : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_MAS_FISCAL_Period d_acm_fiscal_period = new D_ACM_MAS_FISCAL_Period();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();
        D_ACM_GENERAL_Ledger lg = new D_ACM_GENERAL_Ledger();
        MailMerge mailMerge = new MailMerge();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrdBgmSource();
                LoadDDLAccountSeq();
            }
        }

        protected void LoadGrdBgmSource()
        {
            grdBgmSource.DataSource = source.GetList();
            grdBgmSource.DataBind();
        }

        protected void btnReportExcel_Click(object sender, EventArgs e)
        {
            if (!dtpStartDate.GetDateValue().HasValue || !dtpEndDate.GetDateValue().HasValue || grdBgmSource.SelectedItems.Count == 0)
            {
                lblWarning.Visible = true;
                return;
            }

            lblWarning.Visible = false;
            DateTime dtStartDate = dtpStartDate.GetDateValue().Value;
            DateTime dtEndDate = dtpEndDate.GetDateValue().Value;
            string strAccountSeq = ddlAccountSeq.SelectedItem.Text.Split(':')[0].Trim();
            string strAccountSeq2 = ddlAccountSeq2.SelectedItem.Text.Split(':')[0].Trim();
            string strBgmSourceSeq = "";

            for (int i = 0; i < grdBgmSource.SelectedItems.Count; i++)
            {
                GridDataItem item = (GridDataItem)grdBgmSource.SelectedItems[i];
                strBgmSourceSeq += item["bgm_source_seq"].Text + (i == grdBgmSource.SelectedItems.Count - 1 ? "" : ",");
            }

            if (strAccountSeq == "< เลือกทั้งหมด >") strAccountSeq = null;
            if (strAccountSeq2 == "< เลือกทั้งหมด >") strAccountSeq2 = null;

            DataTable dtb = lg.ReportLedger(dtStartDate, dtEndDate, strBgmSourceSeq, strAccountSeq, strAccountSeq2);
            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("B1", DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString());
            dic.Add("A3", "ตั้งแต่วันที่ " + dtStartDate.ToShortDateString() + " ถึงวันที่ " + dtEndDate.ToShortDateString());

            int row = 6;
            string strAccountCode = "";
            double AmountPrev = 0;
            double SumAmountDr = 0;
            double SumAmountCr = 0;

            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                DataRow dr = dtb.Rows[i];
                double AmountDr = main.ConvertNullDouble(dr["amount_dr"]) ?? 0;
                double AmountCr = main.ConvertNullDouble(dr["amount_cr"]) ?? 0;

                if (strAccountCode != dr["account_code"].ToString())
                {
                    // footer
                    if (i > 0)
                    {
                        dic.Add("C" + (row).ToString(), "รวม");
                        dic.Add("D" + (row), (SumAmountDr == 0 ? "" : SumAmountDr.ToString("#,##0.00")));
                        dic.Add("E" + (row), (SumAmountCr == 0 ? "" : SumAmountCr.ToString("#,##0.00")));
                        dic.Add("F" + (row), SumAmountDr - SumAmountCr >= 0 ? (SumAmountDr - SumAmountCr).ToString("#,##0.00") : (SumAmountCr - SumAmountDr).ToString("(#,##0.00)"));

                        row++;
                        row++;
                    }

                    // header
                    dic.Add("B" + (row), dr["account_code"].ToString());
                    dic.Add("C" + (row), dr["account_name"].ToString());

                    strAccountCode = dr["account_code"].ToString();
                    SumAmountDr = main.ConvertNullDouble(dr["prev_amount_dr"]) ?? 0;
                    SumAmountCr = main.ConvertNullDouble(dr["prev_amount_cr"]) ?? 0;
                    AmountPrev = (main.ConvertNullDouble(dr["prev_amount_dr"]) ?? 0) - (main.ConvertNullDouble(dr["prev_amount_cr"]) ?? 0);
                    row++;

                    dic.Add("C" + (row).ToString(), "ยอดยกมา");
                    dic.Add("D" + (row), (SumAmountDr == 0 ? "" : SumAmountDr.ToString("#,##0.00")));
                    dic.Add("E" + (row), (SumAmountCr == 0 ? "" : SumAmountCr.ToString("#,##0.00")));

                    row++;
                }

                if (main.ConvertNullDateTime(dr["tran_date"]).HasValue)
                {
                    dic.Add("A" + (row).ToString(), main.ConvertNullDateTime(dr["tran_date"]).Value.ToShortDateString());
                    dic.Add("B" + (row).ToString(), dr["sub_account_code"].ToString());
                    dic.Add("C" + (row).ToString(), dr["tran_name"].ToString());
                    dic.Add("D" + (row).ToString(), (AmountDr == 0 ? "" : AmountDr.ToString("#,##0.00")));
                    dic.Add("E" + (row).ToString(), (AmountCr == 0 ? "" : AmountCr.ToString("#,##0.00")));
                    dic.Add("F" + (row).ToString(), AmountPrev + AmountDr - AmountCr >= 0 ?
                        (AmountPrev + AmountDr - AmountCr).ToString("#,##0.00") :
                        (-(AmountPrev + AmountDr - AmountCr)).ToString("(#,##0.00)"));


                    dic.Add("H" + (row).ToString(), dr["pay_ref_no"].ToString());
                    dic.Add("I" + (row).ToString(), dr["money_req_ref_no"].ToString());
                    dic.Add("J" + (row).ToString(), dr["expense_amount"].ToString());
                    dic.Add("K" + (row).ToString(), dr["return_amount"].ToString());
                    SumAmountDr += AmountDr;
                    SumAmountCr += AmountCr;
                    AmountPrev = AmountPrev + AmountDr - AmountCr;

                    row++;
                }

                if (i == dtb.Rows.Count - 1)
                {
                    // footer
                    dic.Add("C" + (row).ToString(), "รวม");
                    dic.Add("D" + (row), (SumAmountDr == 0 ? "" : SumAmountDr.ToString("#,##0.00")));
                    dic.Add("E" + (row), (SumAmountCr == 0 ? "" : SumAmountCr.ToString("#,##0.00")));
                    dic.Add("F" + (row), SumAmountDr - SumAmountCr >= 0 ? (SumAmountDr - SumAmountCr).ToString("#,##0.00") : (SumAmountCr - SumAmountDr).ToString("(#,##0.00)"));
                }
            }

            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/Ledger.xlsx"), string.Format("Ledger{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), null, dic);
        }

        protected void LoadDDLAccountSeq()
        {
            var data = new D_ACM_MAS_Account().GetListDataAccountFilter(null);
            ddlAccountSeq.DataSource = data;
            ddlAccountSeq.DataBind();
            ddlAccountSeq2.DataSource = data;
            ddlAccountSeq2.DataBind();

            main.InsertEmptyDDLAll(ddlAccountSeq);
            main.InsertEmptyDDLAll(ddlAccountSeq2);
        }
    }
}
